package com.inoneone.youba.mcht.controller;

import com.inoneone.toolkit.enums.EventType;
import com.inoneone.toolkit.exception.AssertUtil;
import com.inoneone.toolkit.exception.BusinessRuntimeException;
import com.inoneone.youba.mcht.dao.po.Merchant;
import com.inoneone.youba.mcht.dao.po.Tanker;
import com.inoneone.youba.mcht.dto.BaseRsp;
import com.inoneone.youba.mcht.dto.req.*;
import com.inoneone.youba.mcht.dto.rsp.AddOilRsp;
import com.inoneone.youba.mcht.dto.rsp.MyInfoRsp;
import com.inoneone.youba.mcht.dto.rsp.MyTankerRsp;
import com.inoneone.youba.mcht.dto.rsp.SystemInfo;
import com.inoneone.youba.mcht.enums.Constants;
import com.inoneone.youba.mcht.enums.ErrorCode;
import com.inoneone.toolkit.exception.RegExp;
import com.inoneone.youba.mcht.pojo.Event;
import com.inoneone.youba.mcht.service.MerchantApi;
import com.inoneone.youba.mcht.service.SafeApi;
import com.inoneone.youba.mcht.util.CommonUtil;
import com.inoneone.youba.mcht.util.SessionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/mch")
public class MchController {
    @Autowired
    private MerchantApi merchantApi;

    @Autowired
    private SafeApi safeApi;

    /**
     * Description: 系统信息查询</br>
     * Date: 2017-06-20 04:06:10</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    @PostMapping(value = "/sysInfo")
    @ResponseBody
    public Object sysInfo(HttpSession httpSession) {
        //参数检查

        //获取系统信息
        Object o = getSysInfo(httpSession);

        //返回
        return new BaseRsp(o);
    }

    /**
     * Description: 我的信息查询</br>
     * Date: 2017-06-21 11:25:53</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    @PostMapping(value = "/myInfo")
    @ResponseBody
    public Object myInfo(HttpSession httpSession)
    {
        //参数检查

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //返回数据
        MyInfoRsp myInfoRsp = new MyInfoRsp();
        BeanUtils.copyProperties(merchant, myInfoRsp);
        myInfoRsp.setMchNo(merchant.getId());
        myInfoRsp.setMchType(String.valueOf(merchant.getMchType()));
        return new BaseRsp(myInfoRsp);
    }

    @PostMapping(value = "/myTanker")
    @ResponseBody
    public Object myTanker(HttpSession httpSession, @RequestBody MyTankerReq myTankerReq)
    {
        //参数检查

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //查询数据
        List<Tanker> tankers = merchantApi.queryTankerInfo(myTankerReq, merchant.getId());

        //拼装返回
        List<MyTankerRsp> MyTankerRsps = new ArrayList<>();
        for(Tanker tanker : tankers)
        {
            MyTankerRsp myTankerRsp = new MyTankerRsp();
            BeanUtils.copyProperties(tanker, myTankerRsp);
            MyTankerRsps.add(myTankerRsp);
        }
        return new BaseRsp(MyTankerRsps);
    }

    /**
     * Description: 补录油量</br>
     * Date: 2017-06-21 16:06:12</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @PostMapping(value = "/addOil")
    @ResponseBody
    public Object addOil(HttpSession httpSession, @RequestBody AddOilReq addOilReq)
    {
        //参数检查
        AssertUtil.isMatch(addOilReq.getTankerId(), RegExp.TANKER_ID, ErrorCode.TANKER_ID);
        AssertUtil.isMatch(addOilReq.getOil(),RegExp.AMOUNT_YUAN, ErrorCode.ERR_FUEL);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //校验加油机是否属于登录商户
        Tanker tanker = merchantApi.queryTanker(addOilReq.getTankerId());
        if(!tanker.getMchId().equals(merchant.getId()))
        {
            throw new BusinessRuntimeException(ErrorCode.TANKER_MCH_NOT_MATCH);
        }

        //录入油量
        AddOilRsp addOilRsp = merchantApi.addOil(addOilReq, merchant.getId());

        //记录日志
        Event event = new Event();
        event.setType(EventType.FUEL.getCode());
        event.setDesc(tanker.getName() + "["+ tanker.getTankerId() +"]进行了补油操作");
        event.setMchId(merchant.getId());
        event.setMchName(merchant.getMchName());
        event.setTankerId(addOilReq.getTankerId());
        event.setTankerName(tanker.getName());
        safeApi.log(event);

        //拼装返回
        return new BaseRsp(addOilRsp);
    }

    /**
     * Description: 编辑系统信息</br>
     * Date: 2017-06-21 16:06:12</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    @PostMapping(value = "/editMyInfo")
    @ResponseBody
    public Object editMyInfo(HttpSession httpSession, @RequestBody EditMyInfoReq editMyInfoReq)
    {
        //参数检查
        AssertUtil.isMatch(editMyInfoReq.getContactName(), RegExp.REAL_NAME, ErrorCode.ERR_CONTACT_NAME);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //更新信息
        merchantApi.editMyinfo(editMyInfoReq, merchant.getId());

        //更新session缓存
        merchant.setContactName(editMyInfoReq.getContactName());
        CommonUtil.setSession(httpSession, merchant);

        //拼装返回
        return new BaseRsp();
    }

    /**
     * Description: 加油机补录油量</br>
     * Date: 2017-06-21 16:06:12</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @PostMapping(value = "/adjustOil")
    @ResponseBody
    public Object adjustOil(HttpSession httpSession, @RequestBody AddOilReq addOilReq)
    {
        //参数检查
        AssertUtil.isMatch(addOilReq.getTankerId(), RegExp.TANKER_ID, ErrorCode.TANKER_ID);
        AssertUtil.isMatch(addOilReq.getOil(),RegExp.AMOUNT_YUAN_ZERO, ErrorCode.ERR_FUEL);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //校验加油机是否属于登录商户
        checkTanker(addOilReq.getTankerId(), merchant.getId());

        //录入油量
        AddOilRsp addOilRsp = merchantApi.adjustOil(addOilReq);

        //拼装返回
        return new BaseRsp(addOilRsp);
    }

    /**
     * Description: 编辑加油机信息</br>
     * Date: 2017-06-21 16:06:12</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @PostMapping(value = "/editTanker")
    @ResponseBody
    public Object editTanker(HttpSession httpSession, @RequestBody EditTankerReq editTankerReq)
    {
        //参数检查
        AssertUtil.isMatch(editTankerReq.getTankerId(), RegExp.TANKER_ID, ErrorCode.TANKER_ID);
//        AssertUtil.hasText(editTankerReq.getBrands(), ErrorCode.ERR_BRANDS);
//        AssertUtil.hasText(editTankerReq.getModelType(), ErrorCode.ERR_MODEL_TYPE);
//        AssertUtil.hasText(editTankerReq.getName(), ErrorCode.ERR_TRANKER_NAME);
//        AssertUtil.hasText(editTankerReq.getOilType(), ErrorCode.ERR_OIL_TYPE);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //校验加油机是否属于登录商户
        checkTanker(editTankerReq.getTankerId(), merchant.getId());

        //编辑信息
        merchantApi.editTanker(editTankerReq);

        //拼装返回
        return new BaseRsp();
    }

    @PostMapping(value = "/editPrice")
    @ResponseBody
    public Object editPrice(HttpSession httpSession, @RequestBody EditPriceReq editPriceReq)
    {
        //参数检查
        AssertUtil.isMatch(editPriceReq.getTankerId(), RegExp.TANKER_ID, ErrorCode.TANKER_ID);
        AssertUtil.isMatch(editPriceReq.getPrice(), RegExp.AMOUNT_YUAN, ErrorCode.ERR_PRICE);
        AssertUtil.hasText(editPriceReq.getPayPwd(), 32, ErrorCode.PASSWORD_BAD);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //校验加油机是否属于登录商户
        checkTanker(editPriceReq.getTankerId(), merchant.getId());

        //校验支付密码
        if(!editPriceReq.getPayPwd().equals(merchant.getPayPassword()))
        {
            throw new BusinessRuntimeException(ErrorCode.PASSWORD_WRONG);
        }

        //编辑信息
        merchantApi.editPrice(editPriceReq);

        //拼装返回
        return new BaseRsp();
    }


    /**
     * Description: 缓存获取系统信息</br>
     * Date: 2017-06-22 15:10:40</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    private Object getSysInfo(HttpSession httpSession) {
//        Object o = httpSession.getAttribute(Constants.SYS_INFO_KEY);
//        if (o == null)
//        {
//            SystemInfo systemInfo = merchantApi.querySystemInfo(SessionUtil.getSession(httpSession).getId());
//            systemInfo.setMchName(SessionUtil.getSession(httpSession).getMchName());
//            httpSession.setAttribute(Constants.SYS_INFO_KEY, systemInfo);
//            o = systemInfo;
//        }
//        return  o; TODO

        SystemInfo systemInfo = merchantApi.querySystemInfo(SessionUtil.getSession(httpSession).getId());
        systemInfo.setMchName(SessionUtil.getSession(httpSession).getMchName());
        httpSession.setAttribute(Constants.SYS_INFO_KEY, systemInfo);
        return systemInfo;

    }

    /**
     * Description: 校验加油机是否属于登录商户</br>
     * Date: 2017-07-04 11:03:10</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    private  void checkTanker(String tankerId, String mchId)
    {
        Tanker tanker = merchantApi.queryTanker(tankerId);
        if(!tanker.getMchId().equals(mchId))
        {
            throw new BusinessRuntimeException(ErrorCode.TANKER_MCH_NOT_MATCH);
        }
    }

}
